#include <nxos.h>

void EnvLineTest(char *envline)
{
    char *env[64];
    int envc;
    int i;
    char newEnvline[128];
    
    NX_Printf("envline: %s\n", envline);
    NX_MemZero(env, sizeof(env));
    envc = NX_EnvToArray(envline, env, 64);
    NX_Printf("envc: %d for %s\n", envc, envline);
    for (i = 0; i < envc; i++)
    {
        NX_Printf("env[%d]: %s\n", i, env[i]);
    }

    envc = NX_EnvToBuf(newEnvline, 128, env);
    NX_Printf("* envc: %d for %s\n", envc, newEnvline);
}

void CmdLineTest(char *cmdline)
{
    char *cmd[64];
    int argc;
    int i;
    char neCmdline[128];
    
    NX_Printf("cmdline: %s\n", cmdline);
    NX_MemZero(cmd, sizeof(cmd));
    argc = NX_CmdToArray(cmdline, cmd, 64);
    NX_Printf("argc: %d for %s\n", argc, cmdline);
    for (i = 0; i < argc; i++)
    {
        NX_Printf("arg[%d]: %s\n", i, cmd[i]);
    }

    argc = NX_CmdToBuf(neCmdline, 128, cmd);
    NX_Printf("* envc: %d for %s\n", argc, neCmdline);
}

NX_Error TEST_Args(char * _cmdline)
{
    char * envline;
    char * cmdline;
    
    envline = "a";
    EnvLineTest(envline);

    envline = "/a";
    EnvLineTest(envline);

    envline = "/a  :";
    EnvLineTest(envline);

    envline = "/a  ::b";
    EnvLineTest(envline);

    envline = "/a  :/b";
    EnvLineTest(envline);

    envline = "/a  :/c";
    EnvLineTest(envline);


    envline = "/a  :/c:";
    EnvLineTest(envline);

    envline = "/a  ::/c:/d/e";
    EnvLineTest(envline);

    cmdline = "a";
    CmdLineTest(cmdline);

    cmdline = "   abcasd ";
    CmdLineTest(cmdline);

    cmdline = "abcasd asdasd";
    CmdLineTest(cmdline);

    cmdline = "abcasd asdasd|asdl[p";
    CmdLineTest(cmdline);

    cmdline = "abcasd asdasd|asdl[p \"abc\", \"hello, world!\" 134 #";
    CmdLineTest(cmdline);

    return NX_EOK;
}
